﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCLCustomerManagement.DataTransferObjects.Category;

namespace NCLCustomerManagement.DataAccess.Converter
{
    public class CategoryConverter
    {
        private CategoryConverter() { }


        private static CustomerDTO _ToDTO(Category entity)
        {
            if (entity == null)
                return null;

            var dto = new CustomerDTO()
            {
                Alias = entity.Alias,
                CategoryName = entity.CategoryName,
                Id = entity.Id
            };
          
            foreach (Category child in entity.Categories)
            {
                var childDTO = _ToDTO(child);
                childDTO.Parent = dto;
                dto.Children.Add(childDTO);

            }
            return dto;
        }

        public static CustomerDTO ToDTO(Category entity)
        {
            if (entity == null)
                return null;
            var dto = _ToDTO(entity);
            if (entity.ParentCategoryId.HasValue)
            {
                dto.Parent = _ToDTO(entity.Category1);
            }
            return dto;
        }

        public static Category ToEntityShallow(CustomerDTO dto)
        {
            return new Category()
            {
                Id = dto.Id,
                Alias = dto.Alias,
                CategoryName = dto.CategoryName,
                ParentCategoryId = dto.Parent.Id,
            };
        }


    }
}
